import { storeBindingsBehavior } from 'mobx-miniprogram-bindings'
import member from '@src/store/member'
import base from '@src/store/base'
import { dumpFunc } from '@src/common/dump'
import router from "@src/router";
import { getOrderCount } from '@src/api/order'

Component({
  behaviors: [storeBindingsBehavior],
  storeBindings: [{
    store: member,
    fields: ['isMember', 'memberName', 'categoryName', 'categoryImage', 'wxName', 'expireTime','wxHeadImg', 'cardOrder'],
    actions: ['getBaseInfo', 'syncWxCard'],
  }, {
    store: base,
    fields: ['themeColor', 'myPageBanners', 'myPageBg', 'perms', 'isECDS']
  }],
  properties: {
    login: {
      type:Boolean,
      value: false
    },
    modul: {
      type: Object,
      value: {}
    },
    wxCardState: {
      type: Object,
      value: {
        hasGet: false,
			  hasActive: false
      }
    },
    isMember: {
      type: Boolean,
      value: false
    }
  },
  lifetimes: {
    ready() {
      this.fetchData()
      console.log(this.data.modules)
    }
  },
  pageLifetimes: {
    show: function() {
      if(!this.data.login) return
    },
  },
  data: {
    fileBaseUrl: '',
    modules: []
  },

  methods: {
    navigateTo(e) {
      const { value, path, isPath } = e.currentTarget.dataset.item.biz.params
      const { dump } = e.currentTarget.dataset.item
      if(!this.data.login) {
        this.triggerEvent('login'); return;
      }

      
      if (!base.isECDS && path === '/code-order/pages/invoice/main') return wx.showToast({
          title: '商户暂未开通发票功能',
          icon: 'none',
          duration: 2000
      })
      
      if([7].includes(value)) {
        const { contactsType = 0, contactsInfo = {} } = e.currentTarget.dataset.item.biz.params
        console.log(contactsInfo)
        try {
          contactsType === 1 && wx.openCustomerServiceChat({
            extInfo: {
              url: contactsInfo.address
            },
            corpId: contactsInfo.id,
            fail(res) {
              wx.showToast({
                title: '当前无客服', //提示文字
                icon: "none",
                duration: 2000, //显示时长
              })
            },
          })
        } catch (error) {
          wx.showModal({
            content: '当前微信基础库版本过低，此功能暂不支持',
            showCancel: false
          })
        }

        return
      }

      if(![0, 11].includes(value) && !path) {
        wx.showToast({
          title: '该入口还未开通',
          icon: 'none'
        })
        return
      }

      if(value === 0 && isPath) {
        router.push({ path: path })
        return
      }
      switch(value) {
        case 0: 
          dumpFunc(dump)
        return
        case 1:
        const { orderType } = e.currentTarget.dataset.item.biz.params
        router.push({ path: path, params: {orderType} })
        return
        case 11:
          this.triggerEvent('handleOpenWxCard')
        return
      }

      



      router.push({ path: path })
    },
    fetchData() {
      let fileBaseUrl = getApp().globalData.extConfig.fileBaseUrl
      let { modules } = this.data.modul
      const { isMember, login } = this.data
      let module = JSON.parse(JSON.stringify(modules))
      if(!module || !module.length) return
      (!isMember || !login) && (module = module.filter(item => !JSON.parse(item.biz.params)?.isMemberShow))
      module.forEach(item => {
        let params = JSON.parse(item.biz.params)
        const { value, tig, orderType } = params
        if(value === 1 && !tig && this.data.login) {
          getOrderCount({types: orderType.join()})
          .then(res => {
            params.tig = res.data
            item.biz.params = params
            this.setData({modules: module})
          })
        }
        item.biz.params = params
      })
      
      this.setData({fileBaseUrl, modules: module})
    }

  }
})
